package com.CmJava.service.imp;

import com.CmJava.entity.comment.CommentEntity;
import com.CmJava.entity.comment.CommentMysql;
import com.CmJava.entity.ResultEntity;
import com.CmJava.entity.comment.CommentMysqlForBlog;
import com.CmJava.entity.comment.CommentRedis;
import com.CmJava.entity.visitor.Visitor;
import com.CmJava.mapper.BlogCommentMapper;
import com.CmJava.mapper.ProjectCommentMapper;
import com.CmJava.mapper.VisitorMapper;
import com.CmJava.service.CommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private ProjectCommentMapper projectCommentMapper;

    @Autowired
    private BlogCommentMapper blogCommentMapper;

    @Autowired
    private VisitorMapper visitorMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultEntity getCommentsMysql(String projectId) {
        QueryWrapper<CommentMysql> commentMysqlQueryWrapper = new QueryWrapper<>();
        commentMysqlQueryWrapper.eq("projectId",projectId);
        List<CommentMysql> commentMysqls = projectCommentMapper.selectList(commentMysqlQueryWrapper);
        if (commentMysqls==null || commentMysqls.size()<=0){
            return ResultEntity.failureWithoutMessage();
        }
        return ResultEntity.successWithMessage(commentMysqls.get(0));
    }

    @Override
    public ResultEntity getCommentsRedis(String commentKeys) {
        ArrayList<CommentRedis> list = new ArrayList<>();
        if (commentKeys.indexOf("-")==-1){
            CommentRedis commentRedis = (CommentRedis) redisTemplate.opsForValue().get(commentKeys);
            if (commentRedis == null) {
                return ResultEntity.failureWithoutMessage();
            }
            list.add(commentRedis);
        }else {
            String[] commentKeys0 = commentKeys.split("-");
            for (String commentKey : commentKeys0) {
                if (redisTemplate.hasKey(commentKey)) {
                    CommentRedis commentRedis = (CommentRedis) redisTemplate.opsForValue().get(commentKey);
                    if (commentRedis == null) {
                        return ResultEntity.failureWithoutMessage();
                    }
                    list.add(commentRedis);
                } else {
                    return ResultEntity.failureWithoutMessage();
                }
            }
        }
        return ResultEntity.successWithMessage(list);
    }

    @Override
    public ResultEntity sendComment(String key, CommentRedis commentRedis, String projectId) {
        if (!redisTemplate.hasKey(key)) {
            redisTemplate.opsForValue().set(key,commentRedis);

            QueryWrapper<CommentMysql> commentMysqlQueryWrapper = new QueryWrapper<>();
            commentMysqlQueryWrapper.eq("projectId",projectId);
            List<CommentMysql> commentMysqls = projectCommentMapper.selectList(commentMysqlQueryWrapper);
            if (commentMysqls==null || commentMysqls.size()<=0){
                return ResultEntity.failureWithoutMessage();
            }else {
                CommentMysql commentMysql = commentMysqls.get(0);
                // 评论数+1
                commentMysql.setCommentNum(commentMysql.getCommentNum()+1);
                // 增加commentKey
                String commentKey = commentMysql.getCommentKey();
                if (commentKey.equals("--")){
                    commentMysql.setCommentKey(key);
                }else {
                    commentMysql.setCommentKey(commentKey+"-"+key);
                }


                projectCommentMapper.updateById(commentMysql);
            }
            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }

    }

    @Override
    public ResultEntity thumbTa(String redisKey, String commentId, HttpServletRequest request) {
        if (redisTemplate.hasKey(redisKey)) {
            CommentRedis commentRedis = (CommentRedis) redisTemplate.opsForValue().get(redisKey);
            for (CommentEntity comment : commentRedis.getComments()) {
                if (comment.getCommentKey().equals(commentId)){
                    comment.setThumbNum(comment.getThumbNum()+1);
                    break;
                }
            }
            redisTemplate.opsForValue().set(redisKey,commentRedis);

            // 更新该访问者的点赞记录;
            Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
            if (visitor.getThumbKey().equals("--")){
                visitor.setThumbKey(commentId);
                request.getSession().setAttribute("visitor",visitor);
            }else {
                visitor.setThumbKey(visitor.getThumbKey()+"-"+commentId);
                request.getSession().setAttribute("visitor",visitor);
            }
            visitorMapper.updateById(visitor);

            return ResultEntity.successWithoutMessage();

        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }

    @Override
    public ResultEntity replyComment(String redisKey, String commentId, String comment, HttpServletRequest request) {
        if (redisTemplate.hasKey(redisKey)) {
            CommentRedis commentRedis = (CommentRedis) redisTemplate.opsForValue().get(redisKey);
            int commentNum = commentRedis.getComments().size()+1;
            String[] s = commentId.split("_");
            s[2]=String.valueOf(commentNum);
            String newCommentKey="";
            for (int i = 0; i <s.length ; i++) {
                newCommentKey+=s[i];
                if (i!=s.length-1){
                    newCommentKey+="_";
                }
            }

            String commenterName = request.getRemoteAddr();
            if (commenterName.equals("127.0.0.1")){
                commenterName="博主";
            }else {
                commenterName+=" 网友";
            }
            String thisMoment = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            commentRedis.getComments().add(new CommentEntity(newCommentKey,commenterName,comment,thisMoment,0));
            redisTemplate.opsForValue().set(redisKey,commentRedis);


            QueryWrapper<CommentMysql> commentMysqlQueryWrapper = new QueryWrapper<>();
            commentMysqlQueryWrapper.eq("projectId",s[0].substring(1));
            List<CommentMysql> commentMysqls = projectCommentMapper.selectList(commentMysqlQueryWrapper);
            if (commentMysqls==null || commentMysqls.size()<=0){
                return ResultEntity.failureWithoutMessage();
            }else {
                CommentMysql commentMysql = commentMysqls.get(0);
                // 评论数+1
                commentMysql.setCommentNum(commentMysql.getCommentNum()+1);
                projectCommentMapper.updateById(commentMysql);
            }

            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }

    @Override
    public ResultEntity sendBlogComment(String key, CommentRedis commentRedis, String blogId) {
        if (!redisTemplate.hasKey(key)) {
            redisTemplate.opsForValue().set(key,commentRedis);

            QueryWrapper<CommentMysqlForBlog> commentMysqlForBlogQueryWrapper = new QueryWrapper<>();
            commentMysqlForBlogQueryWrapper.eq("blogId",blogId);
            List<CommentMysqlForBlog> commentMysqlForBlogs = blogCommentMapper.selectList(commentMysqlForBlogQueryWrapper);
            if (commentMysqlForBlogs==null || commentMysqlForBlogs.size()<=0){
                return ResultEntity.failureWithoutMessage();
            }else {
                CommentMysqlForBlog commentMysql = commentMysqlForBlogs.get(0);
                // 评论数+1
                commentMysql.setCommentNum(commentMysql.getCommentNum()+1);
                // 增加commentKey
                String commentKey = commentMysql.getCommentKey();
                if (commentKey.equals("--")){
                    commentMysql.setCommentKey(key);
                }else {
                    commentMysql.setCommentKey(commentKey+"-"+key);
                }


                blogCommentMapper.updateById(commentMysql);
            }
            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }

    @Override
    public ResultEntity getCommentsMysqlBlog(String blogId) {
        QueryWrapper<CommentMysqlForBlog> commentMysqlForBlogQueryWrapper = new QueryWrapper<>();
        commentMysqlForBlogQueryWrapper.eq("blogId",blogId);
        List<CommentMysqlForBlog> commentMysqlForBlogs = blogCommentMapper.selectList(commentMysqlForBlogQueryWrapper);
        if (commentMysqlForBlogs!=null && commentMysqlForBlogs.size()>0){
            return ResultEntity.successWithMessage(commentMysqlForBlogs.get(0));
        }
        return ResultEntity.failureWithoutMessage();
    }

    @Override
    public ResultEntity getCommentsRedisBlog(String commentKey) {
        ArrayList<CommentRedis> list = new ArrayList<>();
        if (commentKey.indexOf("-")==-1){
            CommentRedis commentRedis = (CommentRedis) redisTemplate.opsForValue().get(commentKey);
            if (commentRedis == null) {
                return ResultEntity.failureWithoutMessage();
            }
            list.add(commentRedis);
        }else {
            String[] commentKeys0 = commentKey.split("-");
            for (String commentKey0 : commentKeys0) {
                if (redisTemplate.hasKey(commentKey0)) {
                    CommentRedis commentRedis = (CommentRedis) redisTemplate.opsForValue().get(commentKey0);
                    if (commentRedis == null) {
                        return ResultEntity.failureWithoutMessage();
                    }
                    list.add(commentRedis);
                } else {
                    return ResultEntity.failureWithoutMessage();
                }
            }
        }
        return ResultEntity.successWithMessage(list);
    }

    @Override
    public ResultEntity replyCommentBlog(String redisKey, String commentId, String comment, HttpServletRequest request) {
        if (redisTemplate.hasKey(redisKey)) {
            CommentRedis commentRedis = (CommentRedis) redisTemplate.opsForValue().get(redisKey);
            int commentNum = commentRedis.getComments().size()+1;
            // b2_c3_1_for_myweb
            String[] s = commentId.split("_");
            s[2]=String.valueOf(commentNum);
            String newCommentKey="";
            for (int i = 0; i <s.length ; i++) {
                newCommentKey+=s[i];
                if (i!=s.length-1){
                    newCommentKey+="_";
                }
            }

            String commenterName = request.getRemoteAddr();
            if (commenterName.equals("127.0.0.1")){
                commenterName="博主";
            }else {
                commenterName+=" 网友";
            }
            String thisMoment = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            commentRedis.getComments().add(new CommentEntity(newCommentKey,commenterName,comment,thisMoment,0));
            redisTemplate.opsForValue().set(redisKey,commentRedis);


            QueryWrapper<CommentMysqlForBlog> commentMysqlForBlogQueryWrapper = new QueryWrapper<>();
            commentMysqlForBlogQueryWrapper.eq("blogId",s[0].substring(1));
            List<CommentMysqlForBlog> commentMysqlForBlogs = blogCommentMapper.selectList(commentMysqlForBlogQueryWrapper);

            if (commentMysqlForBlogs==null || commentMysqlForBlogs.size()<=0){
                return ResultEntity.failureWithoutMessage();
            }else {
                CommentMysqlForBlog commentMysql = commentMysqlForBlogs.get(0);
                // 评论数+1
                commentMysql.setCommentNum(commentMysql.getCommentNum()+1);
                blogCommentMapper.updateById(commentMysql);
            }

            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }
}
